package org.ycx.upms.client;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Iterator;

/**
 * Created by 杨 on 2017-06-26.
 */
@Service
public class






CustomerAccessDecisionManager implements AccessDecisionManager {
    @Override
    public void decide(Authentication authentication, Object o, Collection<ConfigAttribute> collection) throws AccessDeniedException, InsufficientAuthenticationException {
        if (null == collection || collection.size () <= 0) {
            return;
        } else {
            ConfigAttribute c;
            String needRole;
            for (Iterator<ConfigAttribute> iter = collection.iterator (); iter.hasNext (); ) {
                c = iter.next ();
                needRole = c.getAttribute ();
                for (GrantedAuthority ga : authentication.getAuthorities ()) {//authentication 为在注释1 中循环添加到 GrantedAuthority 对象中的权限信息集合
                    if (needRole.trim ().equals (ga.getAuthority ())) {
                        return;
                    }
                }
            }
            throw new AccessDeniedException ("no right");
        }

    }

    @Override
    public boolean supports(ConfigAttribute configAttribute) {
        return true;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
